package com.llb666.shop.items.api.controller;

import com.llb666.common.core.web.response.BaseResponse;
import com.llb666.common.core.web.response.DataResponse;
import com.llb666.shop.items.application.IndexApplication;
import com.llb666.shop.items.api.vo.CarouselVo;
import com.llb666.shop.items.api.vo.CategoryVo;
import com.llb666.shop.items.api.vo.NewItemsVo;
import com.llb666.shop.items.api.vo.SimpleCategoryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商城首页接口。
 *
 * @author llb666
 */
@Tag(name = "首页展示相关的接口")
@Slf4j
@RestController
@RequestMapping("/index")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IndexController {

    private final IndexApplication indexApplication;

    @Operation(summary = "获取首页轮播图列表")
    @GetMapping("/carousel")
    public DataResponse<List<CarouselVo>> carousel() {
        return DataResponse.ok(indexApplication.carousel());
    }

    @Operation(summary = "查询一级商品类目")
    @GetMapping("/cats")
    public DataResponse<List<SimpleCategoryVo>> category() {
        return DataResponse.ok(indexApplication.category());
    }

    @Operation(summary = "查询二级子分类商品类目")
    @GetMapping("/subCat/{rootCatId}")
    public DataResponse<List<CategoryVo>> subCategory(
            @Parameter(name = "rootCatId", description = "一级类目id", required = true) @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return DataResponse.errorMsg("分类不存在！");
        }
        return DataResponse.ok(indexApplication.subCategory(rootCatId));
    }

    @Operation(summary = "首页滚动加载商品推荐")
    @GetMapping("/sixNewItems/{rootCatId}")
    public DataResponse<List<NewItemsVo>> sixNewItems(@Parameter(name = "rootCatId", description = "一级类目id", required = true)
                                                      @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            return DataResponse.errorMsg("分类不存在！");
        }
        return DataResponse.ok(indexApplication.sixNewItems(rootCatId));
    }


    @Operation(summary = "测试")
    @GetMapping("/msg")
    public BaseResponse test(@RequestParam String msg) {
        log.info(msg);
        return DataResponse.ok(msg);
    }
}
